import os
from os import path
import pandas as pd
import sqlite3


def scaner_file(url):
    conn = sqlite3.connect('VoNR.db')
    # 遍历当前路径下所有文件
    file = os.listdir(url)
    for f in file:
    # 字符串拼接
        real_url = path.join(url, f)
        table_name = getTableName(f)
        print('table_name ' + table_name)
        if get_suffix(f) != 'csv':
            continue
        intoDB(real_url, table_name,conn)
        # 打印出来
        print(real_url)


def intoDB(url, table_name, conn):
    data = pd.read_csv(url, encoding="utf-8")
    print(len(data))
    data.to_sql(table_name, conn, if_exists='append', index=False)


def getTableName(file_url):
    _file_name = file_url  # 文件名称不带后缀
    if '.' in file_url:
        _file_name = file_url.split('.')[0]
    return _file_name


def get_suffix(file_url):
    _suffix = ''  # 后缀
    if '.' in file_url:
        _suffix = file_url.split('.')[-1]
    return _suffix


scaner_file(r'E:\P05_Python_Case_VoNR\DataSet')